package com.itlaobing.annotation.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.logging.Logger;

@Component
@Aspect

public class TimeAspect {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    @Pointcut("com.itlaobing.annotation.service.impl.AopStudentServiceImpl.*(..))")
    private void servicePointcut(){
    }

    @Before("servicePointcut()")
    public void before( JoinPoint point ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "]");
    }

    @Around("servicePointcut()")
    public Object around( ProceedingJoinPoint point ) throws Throwable {
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [ before: " + name + "]");
        Object result = point.proceed() ;
        logger.info( LocalDateTime.now().toString() + " [ after : " + name + "]");
        return result ;
    }

    @AfterThrowing( value = "servicePointcut()" , throwing = "th")
    public void afterThrow( JoinPoint point , Throwable th ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "] : " + th.toString());
    }

    @AfterReturning( value = "servicePointcut()" , returning = "rt")
    public void afterReturn( JoinPoint point , Object rt ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "] : " + rt );
    }

    @After("servicePointcut()")
    public void after( JoinPoint point ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "]");
    }

}
